package com.share.sharedevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.security.utils.SecurityUtils;
import com.share.sharedevice.dto.StationDto;
import com.share.sharedevice.entity.Cabinet;
import com.share.sharedevice.entity.Station;
import com.share.sharedevice.entity.StationLocation;
import com.share.sharedevice.mapper.StationMapper;
import com.share.sharedevice.repository.StationLocationRepository;
import com.share.sharedevice.service.ICabinetService;
import com.share.sharedevice.service.IRegionService;
import com.share.sharedevice.service.IStationService;
import com.share.sharedevice.vo.RegionVo;
import com.share.sharedevice.vo.StationVo;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private StationLocationRepository stationLocationRepository;
    /**
     * 查询站点列表
     *
     * @param stationVo 站点信息
     * @return 站点列表
     */
    @Override
    public List<Station> selectStationList(StationVo stationVo) {

        Station station = new Station();

        BeanUtils.copyProperties(stationVo, station);

        List<Station> stations = stationMapper.selectStationList(station);

        stations.forEach(s -> {
            Long cabinetId = s.getCabinetId();

            if (cabinetId != null) {
                Cabinet cabinet = cabinetService.getById(cabinetId);
                s.setCabinetNo(cabinet.getCabinetNo());
            }else {
               throw  new RuntimeException("编号不能为空");
            }
        });

        return stations;
    }

    /**
     * 根据站点编号查询站点信息
     *
     * @param id 站点编号
     * @return 站点信息
     */
    @Override
    public StationVo getByStationId(Long id) {

        Station station = stationMapper.getByStationId(id);

        StationVo stationVo = new StationVo();

        BeanUtils.copyProperties(station, stationVo);

        Long cabinetId = station.getCabinetId();

        if (cabinetId != null) {
            Cabinet cabinet = cabinetService.getById(cabinetId);
            stationVo.setCabinetNo(cabinet.getCabinetNo());
        }else {
            throw  new RuntimeException("编号不能为空");
        }
        return stationVo;
    }

    /**
     * 删除站点信息
     *
     * @param ids 站点编号
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> ids) {

        stationMapper.deleteByIds(ids);

    }

    /**
     * 添加站点信息
     *
     * @param stationDto 站点信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(StationDto stationDto) {

        Station station = new Station();

        BeanUtils.copyProperties(stationDto, station);

        if(station.getName() == null || "".equals(station.getName())){
            throw new RuntimeException("站点名称不能为空");
        }

        station.setDelFlag("0");

        station.setStatus("1");

        if(station.getCabinetId() == null || station.getCabinetId() <= 0){
            throw new RuntimeException("请选择柜机");
        }

        station.setCreateBy(SecurityUtils.getUsername());

        station.setUpdateBy(SecurityUtils.getUsername());

        station.setCreateTime(new Date());

        station.setUpdateTime(new Date());

        String provinceCodeName = station.getProvinceCode();

        String cityCodeName = station.getCityCode();

        String districtCodeName = station.getDistrictCode();

        String addressName = station.getAddress();

        String fullAddressName = getFullAddressName(provinceCodeName, cityCodeName, districtCodeName, addressName);

        station.setFullAddress(fullAddressName);

        //加入MongoDB
        StationLocation stationLocation = new StationLocation();

        stationLocation.setId(ObjectId.get().toString());

        stationLocation.setStationId(station.getId());

        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));

        stationLocationRepository.save(stationLocation);

        stationMapper.add(station);

    }

    /**
     * 修改站点信息
     *
     * @param stationDto 站点信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByStationId(StationDto stationDto) {

        Station station = new Station();

        BeanUtils.copyProperties(stationDto, station);

        String name = station.getName();

        if(name == null || "".equals(name)){
            throw new RuntimeException("站点名称不能为空");
        }

        String provinceCodeName = station.getProvinceCode();
        String cityCodeName = station.getCityCode();
        String districtCodeName = station.getDistrictCode();
        String addressName = station.getAddress();


        String fullAddressName = getFullAddressName(provinceCodeName, cityCodeName, districtCodeName, addressName);

        station.setFullAddress(fullAddressName);

        station.setUpdateBy(SecurityUtils.getUsername());

        station.setUpdateTime(new Date());

        //修改MongoDB
        StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocationRepository.save(stationLocation);

        stationMapper.updateByStationId(station);
    }

    /**
     * 获取完整地址名字
     *
     * @param
     */
    private String getFullAddressName(String provinceCode, String cityCode, String districtCode, String address){

        RegionVo provinceCodeName = regionService.getByRegionId(provinceCode);
        RegionVo cityCodeName = regionService.getByRegionId(cityCode);
        RegionVo districtCodeName = regionService.getByRegionId(districtCode);

        String FullAddressName = provinceCodeName.getName() + cityCodeName.getName() + districtCodeName.getName() + address;

        return FullAddressName;

    }
}
